package internal

import (
	"github.com/BurntSushi/toml"
	"lol.com/feb214-game/nest.git/log"
	"lol.com/feb214-game/nest.git/tools/fs"
)

type jmgGameConf struct {
	BetTS        int64
	AnnounceTS   int64
	AfterTaxRate float64
	// 点控策略（黑白名单）
	PersonalTactic        bool
	PersonalTacticBetEdge int64 // 点控触发边界
	// 蓄水池策略
	PoolTactic bool
	// 蓄水池策略配置
	SysLoseLimit         float64
	SysLoseLimitProb     float64
	WaterPoolFloor       float64
	WaterPoolFloorProb   float64
	WaterPoolCeiling     float64
	WaterPoolCeilingProb float64
	// 抬牌位置
	CardsCutIndexes      []int
	ReChooseWhenDrawRate float64
	SystemMaxPay         int64
}

type JmgConfigParser struct {
	fs.ParserMixIn
	config *JmgConfig
	Init   bool
}

type JmgConfig struct {
	Game  *jmgGameConf
	Robot *RobotConfig
}

func (dcp *JmgConfigParser) ReloadConfig(path string, init bool) bool {
	modified, lastTs := dcp.CheckModify(path)
	if !modified {
		return false
	}
	var dc JmgConfig
	if _, err := toml.DecodeFile(path, &dc); err != nil {
		if init {
			panic(err)
		} else {
			log.Error("fail to reload %v for %v", path, err)
			return false
		}
	}
	dcp.Lock()
	dcp.config = &dc
	dcp.SetLastModifyTime(path, lastTs)
	dcp.Unlock()
	return true
}
func (dcp *JmgConfigParser) ReloadCallback(value string) error {

	var dc JmgConfig
	if _, err := toml.Decode(value, &dc); err != nil {
		if dcp.Init {
			panic(err)
		} else {
			log.Error("fail to reload %v for %v", value, err)
			return err
		}
	}
	dcp.Lock()
	dcp.config = &dc
	dcp.Init = false
	dcp.Unlock()
	return nil
}

func (dcp *JmgConfigParser) GetConfig() interface{} {
	dcp.RLock()
	defer dcp.RUnlock()
	return dcp.config
}
